package com.ccq.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ccq.entity.BlogTitle;
import com.ccq.entity.Comment;
import com.ccq.service.BlogService;

@Controller(value = "blog")
public class BlogController {

	@Autowired
	private BlogService blogService;

	@RequestMapping("/blogPage")
	public ModelAndView blogPage() {
		ModelAndView mv = new ModelAndView();
		List<BlogTitle> list = blogService.blogPage();
		mv.setViewName("blogPage");
		mv.addObject("blogs", list);
		return mv;
	}
	
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(BlogTitle blog) {
		
		blogService.add(blog);
		
		return "redirect:/blogPage";
	}
	
	@RequestMapping(value="/addComment",method=RequestMethod.POST)
	@ResponseBody
	public Comment addComment(@RequestParam Integer blogId,@RequestParam String comment) {
		
		Comment c = blogService.addComment(blogId,comment);
		
		return c;
	   
	}

}
